
<!DOCTYPE html>
<html lang="zh-Hans" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>教科书式的正则匹配 - 默默默默燃</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="TriDiamond Obsidian,"> 
    <meta name="description" content="一枚前端搬砖队队员的记录册,前言

正则在js中是一类比较特殊的对象，它可以匹配各个场景需要的格式验证，例如邮箱、手机号、用户登录名、密码等等，似乎无处不在，在常见的字符串检索或替换中，我们需要提供一种模式表示检索或替换的规则,"> 
    <meta name="author" content="张白告丶"> 
    <link rel="alternative" href="atom.xml" title="默默默默燃" type="application/atom+xml"> 
    <link rel="icon" href="/img/favicon.png"> 
    <link href="https://fonts.loli.net/css?family=Roboto+Mono|Rubik&display=swap" rel="stylesheet">
    
<link rel="stylesheet" href="//at.alicdn.com/t/font_1429596_nzgqgvnmkjb.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/animate.css/3.7.2/animate.min.css">

    
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/css/share.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/codemirror.min.css">

    
<link rel="stylesheet" href="//cdn.bootcss.com/codemirror/5.48.4/theme/dracula.css">

    
<link rel="stylesheet" href="/css/obsidian.css">

    
<link rel="stylesheet" href="/css/ball-atom.min.css">

<meta name="generator" content="Hexo 4.2.0"></head>


<body class="loading">
    <div class="loader">
        <div class="la-ball-atom la-2x">
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
    </div>
    <span id="config-title" style="display:none">默默默默燃</span>
    <div id="loader"></div>
    <div id="single">
    <div class="scrollbar gradient-bg-rev"></div>
<div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <div class="navigation animated fadeIn fast delay-1s">
        <img id="home-icon" class="icon-home" src="/img/favicon.png" alt="" data-url="https://zhanghao-web.github.io">
        <div id="play-icon" title="Play/Pause" class="iconfont icon-play"></div>
        <h3 class="subtitle">教科书式的正则匹配</h3>
        <div class="social">
            <!--        <div class="like-icon">-->
            <!--            <a href="javascript:;" class="likeThis active"><span class="icon-like"></span><span class="count">76</span></a>-->
            <!--        </div>-->
            <div>
                <div class="share">
                    
                        <a href="javascript:;" class="iconfont icon-share1"></a>
                        <div class="share-component-cc" data-disabled="facebook,douban,linkedin,diandian,tencent,google"></div>
                    
                </div>
            </div>
        </div>
    </div>
</div>

    <div class="section">
        <div class=article-header-wrapper>
    <div class="article-header">
        <div class="article-cover animated fadeIn" style="
            animation-delay: 600ms;
            animation-duration: 1.2s;
            background-image: 
                radial-gradient(ellipse closest-side, rgba(0, 0, 0, 0.65), #100e17),
                url(/img/cover.jpg) ">
        </div>
        <div class="else">
            <p class="animated fadeInDown">
                
                <a href="/categories/Js"><b>「
                    </b>JS<b> 」</b></a>
                
                June 03, 2018
            </p>
            <h3 class="post-title animated fadeInDown"><a href="/2018/06/03/study/%E6%95%99%E7%A7%91%E4%B9%A6%E5%BC%8F%E7%9A%84%E6%AD%A3%E5%88%99%E5%8C%B9%E9%85%8D/" title="教科书式的正则匹配" class="">教科书式的正则匹配</a>
            </h3>
            
            <p class="post-count animated fadeInDown">
                
                <span>
                    <b class="iconfont icon-text2"></b> <i>Words count</i>
                    10k
                </span>
                
                
                <span>
                    <b class="iconfont icon-timer__s"></b> <i>Reading time</i>
                    9 mins.
                </span>
                
                
                
                <span id="busuanzi_container_page_pv">
                    <b class="iconfont icon-read"></b> <i>Read count</i>
                    <span id="busuanzi_value_page_pv">0</span>
                </span>
                
            </p>
            
            
            <ul class="animated fadeInDown post-tags-list" itemprop="keywords"><li class="animated fadeInDown post-tags-list-item"><a class="animated fadeInDown post-tags-list-link" href="/tags/RegExp/" rel="tag">RegExp</a></li></ul>
            
        </div>
    </div>
</div>

<div class="screen-gradient-after">
    <div class="screen-gradient-content">
        <div class="screen-gradient-content-inside">
            <div class="bold-underline-links screen-gradient-sponsor">
                <p>
                    <span class="animated fadeIn delay-1s"></span>
                </p>
            </div>
        </div>
    </div>
</div>

<div class="article">
    <div class='main'>
        <div class="content markdown animated fadeIn">
            <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p><img src="https://user-gold-cdn.xitu.io/2018/5/31/163b417d183f6d30?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="regexp"></p>
<blockquote>
<p>正则在js中是一类比较特殊的对象，它<strong>可以匹配各个场景需要的格式验证，例如邮箱、手机号、用户登录名、密码等等</strong>，似乎无处不在，在常见的字符串检索或替换中，我们需要提供一种模式表示检索或替换的规则，来匹配一系列符合某个句法规则的字符串。</p>
</blockquote>
<p>以下是正则的较为官方的解释：</p>
<blockquote>
<p>正则表达式是用于匹配字符串中字符组合的模式。在 JavaScript中，正则表达式也是对象。这些模式被用于 RegExp 的 <code>exec</code> 和 <code>test</code> 方法, 以及 <code>String</code> 的 <code>match</code>、<code>replace</code>、<code>search</code> 和 <code>split</code> 方法。</p>
</blockquote>
<h2 id="创建正则表达式（以下有两种方式来参考）："><a href="#创建正则表达式（以下有两种方式来参考）：" class="headerlink" title="创建正则表达式（以下有两种方式来参考）："></a>创建正则表达式（以下有两种方式来参考）：</h2><h3 id="使用正则表达式的构造函数（new-的方式来创建"><a href="#使用正则表达式的构造函数（new-的方式来创建" class="headerlink" title="使用正则表达式的构造函数（new 的方式来创建)"></a>使用正则表达式的构造函数（new 的方式来创建)</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> regex = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"a"</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> regex = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="string">"^[a-zA-Z]"</span>,<span class="string">'g'</span>) ;    <span class="comment">//当使用引号时不必再加反斜杠’／’</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> regex = <span class="keyword">new</span> <span class="built_in">RegExp</span>(<span class="regexp">/^[a-zA-Z]/</span>,<span class="string">'gi'</span>);      <span class="comment">//当有斜杠时就 不用再加引号</span></span><br></pre></td></tr></table></figure>
<a id="more"></a>
<h3 id="创建字面量（由两个斜杠’／／’包裹需要匹配的内容"><a href="#创建字面量（由两个斜杠’／／’包裹需要匹配的内容" class="headerlink" title="创建字面量（由两个斜杠’／／’包裹需要匹配的内容)"></a>创建字面量（由两个斜杠’／／’包裹需要匹配的内容)</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> regex = <span class="regexp">/ab/</span> ;</span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> regex=<span class="regexp">/^[a-zA-Z]/</span> ;</span><br></pre></td></tr></table></figure>
<p><font color ="red">这两种方式都可以来创建一个正则表达式，但是相对来说第二种方式更加常用一些</font>。</p>
<h2 id="正则表达式的参数，写在-后面，可混合使用"><a href="#正则表达式的参数，写在-后面，可混合使用" class="headerlink" title="正则表达式的参数，写在//后面，可混合使用"></a>正则表达式的参数，写在//后面，可混合使用</h2><ul>
<li>g   全局匹配;找到所有匹配，而不是在第一个匹配后停止</li>
<li>i    匹配全部大小写</li>
<li>m    多行; 将开始和结束字符（^和$）视为在多行上工作（也就是，分别匹配每一行的开始和结束（由 \n 或 \r 分割），而不只是只匹配整个输入字符串的最开始和最末尾处。</li>
<li>s   与m相反，单行匹配</li>
</ul>
<blockquote>
<p>let regex=/^[a-zA-Z]/gim ;</p>
</blockquote>
<h2 id="正则中常用方法以及字符串中与正则相关的方法"><a href="#正则中常用方法以及字符串中与正则相关的方法" class="headerlink" title="正则中常用方法以及字符串中与正则相关的方法"></a>正则中常用方法以及字符串中与正则相关的方法</h2><h3 id="test-方法检索字符串中的值是否匹配给出的正则规则-返回布尔值-true或false。"><a href="#test-方法检索字符串中的值是否匹配给出的正则规则-返回布尔值-true或false。" class="headerlink" title="test()方法检索字符串中的值是否匹配给出的正则规则,返回布尔值 true或false。"></a>test()方法检索字符串中的值是否匹配给出的正则规则,返回布尔值 true或false。</h3><p>检索”ab”是否匹配前面的正则表达式 /../，正确返回true，不匹配返回false</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/../.test(<span class="string">"ab"</span>);        <span class="comment">// true</span></span><br></pre></td></tr></table></figure>
<h3 id="exec-方法检索字符串中的指定值，如果找到匹配的文本，则返回一个结果的数组，反之返回null"><a href="#exec-方法检索字符串中的指定值，如果找到匹配的文本，则返回一个结果的数组，反之返回null" class="headerlink" title="exec()方法检索字符串中的指定值，如果找到匹配的文本，则返回一个结果的数组，反之返回null"></a>exec()方法检索字符串中的指定值，如果找到匹配的文本，则返回一个结果的数组，反之返回null</h3><p>检索正则表达式/abc/是否有在字符串中defaabc有匹配到，返回匹配到内容，索引值，以及检索的字符串</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">/abc/.exec(<span class="string">"defaabc"</span>)       <span class="comment">//  ["abc", index: 4, input: "defaabc"]</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 检索/qqq/是否在abcdefaabc中匹配到，返回 null</span></span><br><span class="line">/qqq/.exec(<span class="string">"abcdefaabc"</span>)     <span class="comment">// null</span></span><br></pre></td></tr></table></figure>
<h3 id="compile-方法用于改变正则匹配内容"><a href="#compile-方法用于改变正则匹配内容" class="headerlink" title="compile()方法用于改变正则匹配内容"></a>compile()方法用于改变正则匹配内容</h3><p>将/abc/的匹配内容改成后面的字符串的内容</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">/abc/.compile(<span class="string">'def’)    //  /def/</span></span><br></pre></td></tr></table></figure>
<h3 id="split-将字符串分割成字符串数组"><a href="#split-将字符串分割成字符串数组" class="headerlink" title="split()将字符串分割成字符串数组"></a>split()将字符串分割成字符串数组</h3><p>将字符串abcd以/b/中的b分割成字符串数组</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">"abcd"</span>.split(<span class="regexp">/b/</span>);       <span class="comment">// ["a", "cd"]</span></span><br></pre></td></tr></table></figure>
<h3 id="replace-方法用于在字符串中用一些字符替换另一些字符或者替换一个与正则表达式匹配的字符串"><a href="#replace-方法用于在字符串中用一些字符替换另一些字符或者替换一个与正则表达式匹配的字符串" class="headerlink" title="replace()方法用于在字符串中用一些字符替换另一些字符或者替换一个与正则表达式匹配的字符串"></a>replace()方法用于在字符串中用一些字符替换另一些字符或者替换一个与正则表达式匹配的字符串</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//用正则内容/\d\d\d/去匹配字符串12345abcde，将匹配的内容替换成*，并返回替换完成的字符串</span></span><br><span class="line"><span class="string">'12345abcde'</span>.replace(<span class="regexp">/\d\d\d/g</span>,<span class="string">'*'</span>);     <span class="comment">//  "*45abcde"</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 去掉字符串的引号，全局检索'，替换成''空</span></span><br><span class="line"><span class="string">'12345abcde'</span>.replace(<span class="regexp">/'/g</span>,<span class="string">''</span>);     <span class="comment">//12345abcde</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 去掉字符串中所有的空格，tab,换页符，换行符</span></span><br><span class="line"><span class="string">' 12 345ab cd    e '</span>.replace(<span class="regexp">/\s/g</span>,<span class="string">''</span>);     <span class="comment">// "12345abcde"</span></span><br></pre></td></tr></table></figure>
<h3 id="search-用于检索字符串中指定的字符串或与正则表达式相匹配的字符串，返回匹配的字符串的起始位置的索引，反之返回-1"><a href="#search-用于检索字符串中指定的字符串或与正则表达式相匹配的字符串，返回匹配的字符串的起始位置的索引，反之返回-1" class="headerlink" title="search() 用于检索字符串中指定的字符串或与正则表达式相匹配的字符串，返回匹配的字符串的起始位置的索引，反之返回-1"></a>search() 用于检索字符串中指定的字符串或与正则表达式相匹配的字符串，返回匹配的字符串的起始位置的索引，反之返回-1</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">'abcdedfasdfs'</span>.search(<span class="regexp">/d/</span>);      <span class="comment">// 3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 即使是全局匹配，也只会匹配到第一个的位置</span></span><br><span class="line"><span class="string">'abcdedfasdfs'</span>.search(<span class="regexp">/d/g</span>);     <span class="comment">// 3</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 如果没有匹配到，返回-1</span></span><br><span class="line"><span class="string">'abcdedfasdfs'</span>.search(<span class="regexp">/o/g</span>);     <span class="comment">// -1</span></span><br></pre></td></tr></table></figure>
<h3 id="match-方法可在字符串内检索指定的值，或找到一个或多个正则表达式的匹配"><a href="#match-方法可在字符串内检索指定的值，或找到一个或多个正则表达式的匹配" class="headerlink" title="match()方法可在字符串内检索指定的值，或找到一个或多个正则表达式的匹配"></a>match()方法可在字符串内检索指定的值，或找到一个或多个正则表达式的匹配</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 检索指定的值，并返回,b+指多个</span></span><br><span class="line"><span class="string">"aabbbbccbbaab"</span>.match(<span class="regexp">/b+/g</span>);     <span class="comment">//  ["bbbb", "bb", "b"]</span></span><br></pre></td></tr></table></figure>
<h2 id="正则规则（常用"><a href="#正则规则（常用" class="headerlink" title="正则规则（常用)"></a>正则规则（常用)</h2><h3 id="字符类规则"><a href="#字符类规则" class="headerlink" title="字符类规则"></a>字符类规则</h3><p><img src="https://user-gold-cdn.xitu.io/2018/5/29/163aad82a90e33a8?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="字符类"></p>
<h3 id="范围符号匹配规则"><a href="#范围符号匹配规则" class="headerlink" title="范围符号匹配规则"></a>范围符号匹配规则</h3><p><img src="https://user-gold-cdn.xitu.io/2018/5/30/163aee18e6b545ee?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="范围符号匹配规则"></p>
<h3 id="分组匹配规则"><a href="#分组匹配规则" class="headerlink" title="分组匹配规则"></a>分组匹配规则</h3><p><img src="https://user-gold-cdn.xitu.io/2018/5/30/163aee220257fe90?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="分组匹配规则"></p>
<h3 id="重复匹配规则"><a href="#重复匹配规则" class="headerlink" title="重复匹配规则"></a>重复匹配规则</h3><p><img src="https://user-gold-cdn.xitu.io/2018/5/30/163aee281db6f27b?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="重复匹配规则"></p>
<p><a href="https://juejin.im/post/5b0cf331f265da0905017b48?utm_source=gold_browser_extension" target="_blank" rel="noopener">原文地址</a></p>

            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls"
                data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
            <ul id="audio-list" style="display:none">
                
                
                <li title='0' data-url='/statics/chengdu.mp3'></li>
                
                    
            </ul>
            
            
            
    <div id='gitalk-container' class="comment link"
        data-ae='true'
        data-ci='ec894e2b66f752e8b7fb'
        data-cs='3ccc2e92bb350688fe2c2dc2930189b62622bfb1'
        data-r='blog-comments'
        data-o='TriDiamond'
        data-a='TriDiamond'
        data-d='undefined'
    >Comments</div>


            
            
        </div>
        <div class="sidebar">
            <div class="box animated fadeInRight">
                <div class="subbox">
                    <img src="https://res.cloudinary.com/tridiamond/image/upload/v1573019751/TriDiamond_logo_ui_xeublz.jpg" height=300 width=300></img>
                    <p>张白告丶</p>
                    <span>Think like an artist, develop like an artisan</span>
                    <dl>
                        <dd><a href="https://github.com/zhanghao-web" target="_blank"><span
                                    class=" iconfont icon-github"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-twitter"></span></a></dd>
                        <dd><a href="" target="_blank"><span
                                    class=" iconfont icon-stack-overflow"></span></a></dd>
                    </dl>
                </div>
                <ul>
                    <li><a href="/">149 <p>Articles</p></a></li>
                    <li><a href="/categories">23 <p>Categories</p></a></li>
                    <li><a href="/tags">47 <p>Tags</p></a></li>
                </ul>
            </div>
            
            
            
            <div class="box sticky animated fadeInRight faster">
                <div id="toc" class="subbox">
                    <h4>Contents</h4>
                    <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#创建正则表达式（以下有两种方式来参考）："><span class="toc-number">1.1.</span> <span class="toc-text">创建正则表达式（以下有两种方式来参考）：</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#使用正则表达式的构造函数（new-的方式来创建"><span class="toc-number">1.1.1.</span> <span class="toc-text">使用正则表达式的构造函数（new 的方式来创建)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#创建字面量（由两个斜杠’／／’包裹需要匹配的内容"><span class="toc-number">1.1.2.</span> <span class="toc-text">创建字面量（由两个斜杠’／／’包裹需要匹配的内容)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#正则表达式的参数，写在-后面，可混合使用"><span class="toc-number">1.2.</span> <span class="toc-text">正则表达式的参数，写在&#x2F;&#x2F;后面，可混合使用</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#正则中常用方法以及字符串中与正则相关的方法"><span class="toc-number">1.3.</span> <span class="toc-text">正则中常用方法以及字符串中与正则相关的方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#test-方法检索字符串中的值是否匹配给出的正则规则-返回布尔值-true或false。"><span class="toc-number">1.3.1.</span> <span class="toc-text">test()方法检索字符串中的值是否匹配给出的正则规则,返回布尔值 true或false。</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#exec-方法检索字符串中的指定值，如果找到匹配的文本，则返回一个结果的数组，反之返回null"><span class="toc-number">1.3.2.</span> <span class="toc-text">exec()方法检索字符串中的指定值，如果找到匹配的文本，则返回一个结果的数组，反之返回null</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#compile-方法用于改变正则匹配内容"><span class="toc-number">1.3.3.</span> <span class="toc-text">compile()方法用于改变正则匹配内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#split-将字符串分割成字符串数组"><span class="toc-number">1.3.4.</span> <span class="toc-text">split()将字符串分割成字符串数组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#replace-方法用于在字符串中用一些字符替换另一些字符或者替换一个与正则表达式匹配的字符串"><span class="toc-number">1.3.5.</span> <span class="toc-text">replace()方法用于在字符串中用一些字符替换另一些字符或者替换一个与正则表达式匹配的字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#search-用于检索字符串中指定的字符串或与正则表达式相匹配的字符串，返回匹配的字符串的起始位置的索引，反之返回-1"><span class="toc-number">1.3.6.</span> <span class="toc-text">search() 用于检索字符串中指定的字符串或与正则表达式相匹配的字符串，返回匹配的字符串的起始位置的索引，反之返回-1</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#match-方法可在字符串内检索指定的值，或找到一个或多个正则表达式的匹配"><span class="toc-number">1.3.7.</span> <span class="toc-text">match()方法可在字符串内检索指定的值，或找到一个或多个正则表达式的匹配</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#正则规则（常用"><span class="toc-number">1.4.</span> <span class="toc-text">正则规则（常用)</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#字符类规则"><span class="toc-number">1.4.1.</span> <span class="toc-text">字符类规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#范围符号匹配规则"><span class="toc-number">1.4.2.</span> <span class="toc-text">范围符号匹配规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#分组匹配规则"><span class="toc-number">1.4.3.</span> <span class="toc-text">分组匹配规则</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#重复匹配规则"><span class="toc-number">1.4.4.</span> <span class="toc-text">重复匹配规则</span></a></li></ol></li></ol></li></ol>
                </div>
            </div>
            
            
        </div>
    </div>
</div>

    </div>
</div>
    <div id="back-to-top" class="animated fadeIn faster">
        <div class="flow"></div>
        <span class="percentage animated fadeIn faster">0%</span>
        <span class="iconfont icon-top02 animated fadeIn faster"></span>
    </div>
</body>
<footer>
    <p class="copyright" id="copyright">
        &copy; 2020
        <span class="gradient-text">
            张白告丶
        </span>.
        Powered by <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a>
        Theme
        <span class="gradient-text">
            <a href="https://github.com/TriDiamond/hexo-theme-obsidian" title="Obsidian" target="_blank" rel="noopener">Obsidian</a>
        </span>
        <small><a href="https://github.com/TriDiamond/hexo-theme-obsidian/blob/master/CHANGELOG.md" title="v1.4.3" target="_blank" rel="noopener">v1.4.3</a></small>
    </p>
</footer>

<script type="text/javascript" src="https://cdn.bootcss.com/mathjax/2.7.6/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
  MathJax.Hub.Config({
    "HTML-CSS": {
      preferredFont: "TeX",
      availableFonts: ["STIX", "TeX"],
      linebreaks: {
        automatic: true
      },
      EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50)
    },
    tex2jax: {
      inlineMath: [
        ["$", "$"],
        ["\\(", "\\)"]
      ],
      processEscapes: true,
      ignoreClass: "tex2jax_ignore|dno",
      skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    },
    TeX: {
      noUndefined: {
        attributes: {
          mathcolor: "red",
          mathbackground: "#FFEEEE",
          mathsize: "90%"
        }
      },
      Macros: {
        href: "{}"
      }
    },
    messageStyle: "none"
  });
</script>
<script>
  function initialMathJax() {
    MathJax.Hub.Queue(function () {
      var all = MathJax.Hub.getAllJax(),
        i;
      // console.log(all);
      for (i = 0; i < all.length; i += 1) {
        console.log(all[i].SourceElement().parentNode)
        all[i].SourceElement().parentNode.className += ' has-jax';
      }
    });
  }

  function reprocessMathJax() {
    if (typeof MathJax !== 'undefined') {
      MathJax.Hub.Queue(["Typeset", MathJax.Hub]);
    }
  }
</script>



    
<link rel="stylesheet" href="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.css">

    
<script src="//cdn.bootcss.com/gitalk/1.5.0/gitalk.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="/js/plugin.js"></script>
<script src="/js/obsidian.js"></script>
<script src="/js/jquery.truncate.js"></script>
<script src="/js/search.js"></script>


<script src="//cdn.bootcss.com/typed.js/2.0.10/typed.min.js"></script>


<script src="//cdn.bootcss.com/blueimp-md5/2.12.0/js/md5.min.js"></script>


<script src="//cdnjs.cloudflare.com/ajax/libs/social-share.js/1.0.16/js/social-share.min.js"></script>


<script src="https://cdn.bootcss.com/codemirror/5.48.4/codemirror.min.js"></script>

    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/javascript/javascript.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/css/css.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/xml/xml.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/htmlmixed/htmlmixed.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/clike/clike.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/php/php.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/shell/shell.min.js"></script>


    
<script src="//cdn.bootcss.com/codemirror/5.48.4/mode/python/python.min.js"></script>




    
<script src="/js/busuanzi.min.js"></script>

    <script>
        $(document).ready(function () {
            if ($('span[id^="busuanzi_"]').length) {
                initialBusuanzi();
            }
        });
    </script>



<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.css">
<link rel="stylesheet" href="//cdn.bootcss.com/photoswipe/4.1.3/default-skin/default-skin.min.css">


<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe.min.js"></script>
<script src="//cdn.bootcss.com/photoswipe/4.1.3/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="//www.googletagmanager.com/gtag/js?id=UA-149874671-1"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        gtag('config', 'UA-149874671-1');
    </script>





<script>
    function initialTyped () {
        var typedTextEl = $('.typed-text');
        if (typedTextEl && typedTextEl.length > 0) {
            var typed = new Typed('.typed-text', {
                strings: ["Think like an artist, develop like an artisan", "艺术家思维去思考问题，工匠创造精神去开发"],
                typeSpeed: 90,
                loop: true,
                loopCount: Infinity,
                backSpeed: 20,
            });
        }
    }

    if ($('.article-header') && $('.article-header').length) {
        $(document).ready(function () {
            initialTyped();
        });
    }
</script>




</html>
